//
//  PicturePlayView.m
//  AVFloatView
//
//  Created by hulinhu on 2020/10/23.
//

#import "PicturePlayView.h"
#import <AVKit/AVKit.h>
#import "PiPViewManager.h"

@interface  PicturePlayView ()<AVPictureInPictureControllerDelegate>

@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPictureInPictureController *pipController;

@end


@implementation PicturePlayView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];

        // 切换按钮
        UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        changeButton .frame = CGRectMake(20, 0, 40, 40);
        UIImage *startImage = [AVPictureInPictureController pictureInPictureButtonStartImage];
        [changeButton setImage:startImage forState:UIControlStateNormal];
        [changeButton addTarget:self action:@selector(clickPipBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:changeButton];
        
        //播放器
        UIView *videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, frame.size.width, frame.size.height-50)];
        videoView.backgroundColor = [UIColor whiteColor];
        [self addSubview:videoView];
        
        // 播放图层
        AVPlayerLayer *playerLayer =  [[AVPlayerLayer alloc] init];
        playerLayer.videoGravity =  AVLayerVideoGravityResizeAspect;
        playerLayer.frame = videoView.bounds;
        [videoView.layer addSublayer:playerLayer];
        self.playerLayer = playerLayer;
        
        // 画中画配置
        if ([AVPictureInPictureController isPictureInPictureSupported]) {
            self.pipController = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer];
            self.pipController.delegate = self;
            NSLog(@"当前设备不支持PiP");
        }
    }
    
    return self;
}

- (void)startPlayer {
    
    NSString *mp4Url = @"https://vd2.bdstatic.com/mda-kjkpvdnzxngm6s5s/sc/cae_h264_clips/1603271118/mda-kjkpvdnzxngm6s5s.mp4";
    NSString  *playUrlString = mp4Url;
    AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:playUrlString]];
    self.playerLayer.player = player;
    [player play];
}

-(void)stopPlay {
    [self.playerLayer.player pause];
}

- (void)clickPipBtn:(UIButton *)sender {
    //  pip 状态
    BOOL isActive =  [self.pipController isPictureInPictureActive];
    NSLog(@" isActive --- %d",isActive);
    if (isActive) {
        [self.pipController stopPictureInPicture];
        UIImage *startImage = [AVPictureInPictureController pictureInPictureButtonStartImage];
        [sender setImage:startImage forState:UIControlStateNormal];
    }else {
        [self.pipController startPictureInPicture];
        UIImage *endImage = [AVPictureInPictureController pictureInPictureButtonStopImage];
        [sender setImage:endImage forState:UIControlStateNormal];
    }
    
}

#pragma mark
#pragma mark - delegate

- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    
    [PiPViewManager sharedSingleton].pipPlayView = self;
    NSLog(@"将要开始PictureInPicture的代理方法");
//    [self.navigationController popViewControllerAnimated:YES];
}

- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"已经开始PictureInPicture的代理方法");
}

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"启动PictureInPicture失败的代理方法");
}

- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"将要停止PictureInPicture的代理方法");
}

- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"已经停止PictureInPicture的代理方法");
}

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
    //此方法执行在pictureInPictureControllerWillStopPictureInPicture代理方法之后，在pictureInPictureControllerDidStopPictureInPicture执行之前。 但是点击“X”移除画中画时，不执行此方法。
    NSLog(@"PictureInPicture停止之前恢复用户界面");
}

-(void)dealloc {
    NSLog(@"%s", __func__);
}



@end
